import { constantRoutes, resetRouter } from '@/router'
import Layout from '@/layout'
// import { SunRouterPath } from '@/router/path'
import { SunRouterComponent } from '@/router/component'
const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}
const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise(resolve => {
      resetRouter()
      commit('SET_ROUTES', setData(roles, false))
      resolve(setData(roles, false))
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
function firstLower(str) { // 首字母转小写
  return str.replace(/^./, (L) => L.toLowerCase())
}
function setData(data, itemName) {
  data?.forEach((item) => {
    // evaluation_status 流程菜单标识
    if (!itemName && item.children.length) { // 路由重定向
      // eslint-disable-next-line no-prototype-builtins
      const redirectPath = '/' + firstLower(item.name) + '/' + firstLower(item.children[0].name) + (item.children[0].hasOwnProperty('evaluation_status') ? '/' + item.children[0].evaluation_status : '')
      item['redirect'] = redirectPath
    }
    // eslint-disable-next-line no-prototype-builtins
    if (itemName && item.hasOwnProperty('evaluation_status')) { // 流程菜单
      const processPath = firstLower(item.name) + '/' + item.evaluation_status
      item['path'] = processPath
      item['component'] = SunRouterComponent[itemName + '.' + item.name]
      item['meta'] = { 'title': item.title || item.name, icon: item.icon || '', request_apis: item.request_apis, params: item.params ? item.params : {}, organization: item.organization || '' }
    } else { // 非流程菜单
      const path = itemName ? '/' + firstLower(itemName) + '/' + firstLower(item.name) : '/' + firstLower(item.name)
      item['path'] = path
      item['component'] = item.children ? Layout : SunRouterComponent[itemName + '.' + item.name]
      item['meta'] = { 'title': item.title || item.name, icon: item.icon || '', params: item.params ? item.params : {}}
    }
    item.children && item.children.length > 0 ? setData(item.children, item.name) : delete item.children
  })
  return data
}
// function setData(data, itemName) {
//   data?.forEach((item) => {
//     // evaluation_status 流程菜单标识
//     if (!itemName) { // 路由重定向
//       // eslint-disable-next-line no-prototype-builtins
//       item['redirect'] = SunRouterPath[item.name] + '/' + SunRouterPath[item.name + '.' + item.children[0].name] + (item.children[0].hasOwnProperty('evaluation_status') ? '/' + item.children[0].evaluation_status : '')
//     }
//     // eslint-disable-next-line no-prototype-builtins
//     if (itemName && item.hasOwnProperty('evaluation_status')) { // 流程菜单
//       item['path'] = SunRouterPath[itemName + '.' + item.name] + '/' + item.evaluation_status
//       item['component'] = SunRouterComponent[itemName + '.' + item.name]
//       item['meta'] = { 'title': item.title || item.name, icon: item.icon || '', request_apis: item.request_apis, params: item.params ? item.params : {}}
//     } else { // 非流程菜单
//       item['path'] = itemName ? SunRouterPath[itemName + '.' + item.name] : SunRouterPath[item.name]
//       item['component'] = item.children ? Layout : SunRouterComponent[itemName + '.' + item.name]
//       item['meta'] = { 'title': item.title || item.name, icon: item.icon || '', params: item.params ? item.params : {}}
//     }
//     item.children && item.children.length > 0 ? setData(item.children, item.name) : delete item.children
//   })
//   return data
// }
